package  
{
	import org.flixel.FlxG;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	import org.flixel.system.input.Keyboard;
	
	
	/**
	 * ...
	 * @author Ian Guthridge
	 */
	public class PlayState extends FlxState 
	{
		[Embed(source = "assets/BearClaw_1_Left.png")]
		private static var claw1Left:Class;
		[Embed(source = "assets/BearClaw_1_Right.png")]
		private static var claw1Right:Class;
		[Embed(source = "assets/BearClaw_2_Left.png")]
		private static var claw2Left:Class;
		[Embed(source = "assets/BearClaw_2_Right.png")]
		private static var claw2Right:Class;
		
		
		[Embed(source = "assets/BackgroundForest.png")]
		private static var bg:Class;
		
		protected var delayTimer:Number;
		
		protected var enemy:Enemy;
		
		protected var arm1_high:FlxSprite;
		protected var arm2_high:FlxSprite;
		
		protected var arm1_low:FlxSprite;
		protected var arm2_low:FlxSprite;
		
		
		protected var scoreText:FlxText;
		
		protected var counter:int;
		protected var healthBorder:FlxSprite;
		protected var healthBar:FlxSprite;
		protected var damage:FlxSprite;
		
		private var playerHealth:Number; //value 0-100 where 100 is full health
		
		private var score:Number;
		
		public function PlayState() 
		{
			this.add(new FlxSprite(0,0, bg));
			scoreText = new FlxText(10,80,1000);
			healthBorder = new FlxSprite(80,20);
			healthBar = new FlxSprite(90,25);
			healthBorder.makeGraphic(500,40);
			healthBar.makeGraphic(480,30);
			healthBar.color = 0x00FF00;
			healthBar.origin = new FlxPoint(0,0);
			this.add(scoreText);
			this.add(healthBorder);
			this.add(healthBar);
			counter = 0;
			playerHealth = 100;
			
			damage = new FlxSprite(0,0);
			enemy = new Enemy(damage);
			this.add(enemy);
			this.add(damage);
			
			arm1_high = new FlxSprite(-175, 300, claw1Left);
			this.add(arm1_high);
			arm2_high = new FlxSprite(700, 300, claw1Right);
			this.add(arm2_high);
			
			arm1_low = new FlxSprite(-100, 300, claw2Left);
			this.add(arm1_low);
			arm2_low = new FlxSprite(800, 300, claw2Right);
			this.add(arm2_low);
			
			delayTimer = 0;
			
			score = 0;
			scoreText.size = 1000;
		}
		
		public function lose():void
		{
			var loseText:FlxText = new FlxText(270,90,800);
			loseText.text = "YOU GOT TRANQ'D";
			loseText.size = 10000000;
			this.add(loseText);
			FlxG.paused = true;
			scoreText.visible = false;
		}
		
		override public function update():void 
		{
			super.update();
			var keys:Keyboard = FlxG.keys;
			
			var q1Keys:int = 0;
			var q2Keys:int = 0;
			var q3Keys:int = 0;
			var q4Keys:int = 0;
			
			if (keys.ONE) ++q1Keys;			
			if (keys.TWO) ++q1Keys;			
			if (keys.THREE) ++q1Keys;			
			if (keys.FOUR) ++q1Keys;			
			if (keys.FIVE) ++q1Keys;			
			if (keys.SIX) ++q1Keys;			
			if (keys.SEVEN) ++q2Keys;			
			if (keys.EIGHT) ++q2Keys;			
			if (keys.NINE) ++q2Keys;			
			if (keys.ZERO) ++q2Keys;			
			if (keys.MINUS) ++q2Keys;			
			if (keys.PLUS) ++q2Keys;
			if (keys.TAB) ++q1Keys;
			if (keys.Q) ++q1Keys;
			if (keys.W) ++q1Keys;
			if (keys.E) ++q1Keys;
			if (keys.R) ++q1Keys;
			if (keys.T) ++q1Keys;			
			if (keys.Y) ++q2Keys;			
			if (keys.U) ++q2Keys;			
			if (keys.I) ++q2Keys;			
			if (keys.O) ++q2Keys;			
			if (keys.P) ++q2Keys;						
			if (keys.LBRACKET) ++q2Keys;					
			if (keys.RBRACKET) ++q2Keys;					
			if (keys.BACKSLASH) ++q2Keys;				
			if (keys.A) ++q3Keys;			
			if (keys.S) ++q3Keys;			
			if (keys.D) ++q3Keys;			
			if (keys.F) ++q3Keys;			
			if (keys.G) ++q3Keys;			
			if (keys.H) ++q4Keys;			
			if (keys.J) ++q4Keys;			
			if (keys.K) ++q4Keys;			
			if (keys.L) ++q4Keys;			
			if (keys.SEMICOLON) ++q4Keys;			
			if (keys.QUOTE) ++q4Keys;			
			if (keys.Z) ++q3Keys;			
			if (keys.X) ++q3Keys;			
			if (keys.C) ++q3Keys;			
			if (keys.V) ++q3Keys;			
			if (keys.B) ++q3Keys;			
			if (keys.N) ++q4Keys;			
			if (keys.M) ++q4Keys;			
			if (keys.COMMA) ++q4Keys;			
			if (keys.PERIOD) ++q4Keys;						
			if (keys.SLASH) ++q4Keys;
			var quad:int = 0;
			var hitStrength:int = 0;
			
			if (q1Keys == 0 && q2Keys == 0 && q3Keys == 0 && q4Keys == 0)
			{
				quad = 0;
			}			
			else if (q1Keys >  q2Keys)
			{
				if (q1Keys >= q3Keys)
				{
					if (q1Keys >= q4Keys)
					{
						quad = 1;
						hitStrength = q1Keys - (q2Keys + q3Keys + q4Keys);						
					}
					else
					{
						quad = 4;
						hitStrength = q4Keys - (q1Keys + q3Keys + q4Keys);
					}
				}
				else
				{
					if (q3Keys >= q4Keys)
					{
						quad = 3;
						hitStrength = q3Keys - (q2Keys + q1Keys + q4Keys);
					}
					else
					{
						quad = 4;
						hitStrength = q4Keys - (q1Keys + q3Keys + q2Keys);
					}
				}
			}
			else 
			{
				if (q2Keys >= q3Keys)
				{
					if (q2Keys >= q4Keys)
					{
						quad = 2;
						hitStrength = q2Keys - (q1Keys + q3Keys + q4Keys);
					}
					else
					{
						quad = 4;
						hitStrength = q4Keys - (q1Keys + q3Keys + q2Keys);
					}
				}
				else
				{
					if (q3Keys >= q4Keys)
					{
						quad = 3;
						hitStrength = q3Keys - (q2Keys + q1Keys + q4Keys);
					}
					else
					{
						quad = 4;
						hitStrength = q4Keys - (q1Keys + q3Keys + q2Keys);
					}
				}
			}
			if(!FlxG.paused)
			{
				if (delayTimer <= 0)
				{
					switch(quad)
					{
						case 0:
							arm1_high.x = -175;
							arm1_high.y = 300;
							arm2_high.x = 700;
							arm2_high.y = 300;
							arm1_high.visible = true;
							arm2_high.visible = true;
							arm1_low.visible = false;
							arm2_low.visible = false;
							break;
						case 1: 
							arm1_high.visible = true;
							arm2_high.visible = false;
							arm1_low.visible = false;
							arm2_low.visible = false;
							arm1_high.x = 0;
							arm1_high.y = 0;
							break;
						case 2:
							arm1_high.visible = false;
							arm2_high.visible = true;
							arm1_low.visible = false;
							arm2_low.visible = false;
							arm2_high.x = 500;
							arm2_high.y = 0;
							break;
						case 3:
							arm1_high.visible = false;
							arm2_high.visible = false;
							arm1_low.visible = true;
							arm2_low.visible = false;
							arm1_low.x = -100;
							arm1_low.y = 200;
							break;
						case 4:
							arm1_high.visible = false;
							arm2_high.visible = false;
							arm1_low.visible = false;
							arm2_low.visible = true;
							arm2_low.x = 400;
							arm2_low.y = 200;
							break;
					}
					if(quad != 0)
					{
						if (!enemy.hit(quad, hitStrength))
						{
							delayTimer = 1;
							FlxG.shake();	
							playerHealth = Math.max(playerHealth - 20,0);				
						}
						else
						{
							score += 1000;
							scoreText.text = "score: " + score;
						}
					}
			
					healthBar.scale = new FlxPoint(playerHealth/100,1);
					
					if(playerHealth <= 30)
					{
						healthBar.color = 0xFF0000;
					
						if (playerHealth <= 0)
						{
							this.lose();
						}
					}
				
					if (!enemy.alive)
					{
						var winText:FlxText = new FlxText(270,90,800);
						winText.text = "A WINNAR IS YOU";
						winText.size = 10000000;
						this.add(winText);
						FlxG.paused = true;
						scoreText.visible = false;
					}
				}
				else
				{
					arm1_high.visible = false;
					arm2_high.visible = false;
					arm1_low.visible = false;
					arm2_low.visible = false;
					delayTimer -= FlxG.elapsed;
				}
			}		
		}	
	}
}